package com.mogist.hqc.module.personal.ui

import android.content.Intent
import android.os.Bundle
import android.support.v7.widget.LinearLayoutManager
import com.chad.library.adapter.base.BaseQuickAdapter
import com.mogist.hqc.R
import com.mogist.hqc.base.BaseMVPFragment
import com.mogist.hqc.entitys.BaseModel
import com.mogist.hqc.entitys.response.CollectListVo
import com.mogist.hqc.entitys.response.CollectVo
import com.mogist.hqc.module.personal.adapter.CollectAdapter
import com.mogist.hqc.module.personal.contract.CollectContract
import com.mogist.hqc.module.personal.presenter.CollectPresenter
import com.mogist.hqc.module.place.ui.ScenicActivity
import com.mogist.hqc.module.product.ui.ProductDetailActivity
import com.mogist.hqc.module.route.ui.RouteMainActivity
import com.mogist.hqc.mvp.HttpDto
import com.mogist.hqc.util.Constant
import kotlinx.android.synthetic.main.fragment_order.*
import org.greenrobot.eventbus.Subscribe
import talex.zsw.basecore.util.LogTool
import talex.zsw.basecore.view.other.swipetoloadlayout.OnLoadMoreListener
import talex.zsw.basecore.view.other.swipetoloadlayout.OnRefreshListener
import talex.zsw.basecore.view.recyleview.DividerItemDecoration
import talex.zsw.basecore.view.recyleview.SampleFooter
import talex.zsw.basecore.view.recyleview.SampleHeader

/**
 * 作用：收藏列表
 * 作者：赵小白 email:vvtale@gmail.com  
 * 修改人：
 * 修改时间：
 * 修改备注：
 */
class CollectFragment : BaseMVPFragment<CollectContract.Presenter>(), CollectContract.View, OnRefreshListener, OnLoadMoreListener
{
    private var position = 0 // "地点","线路","玩家产品"

    override fun initArgs(bundle: Bundle?)
    {
        bundle?.getInt("position")
                ?.let {
                    position = it
                }
    }

    override fun initView(bundle: Bundle?)
    {
        setContentView(R.layout.fragment_collect)
        mPresenter = CollectPresenter(this)
    }

    override fun initData()
    {
        initRecyclerView()
    }

    override fun onResume()
    {
        if (isRefresh)
        {
            onRefresh()
        }
        super.onResume()
    }

    override fun onVisible()
    {
        LogTool.nd("onVisible = " + position)
        if (adapter.itemCount == 0)
        {
            getData(false)
        }
    }

    private val adapter = CollectAdapter()

    private var curPos = 0
    private fun initRecyclerView()
    {
        adapter.setOnItemChildClickListener { adapter, view, pos ->
            if (!isFastClick)
            {
                val vo = adapter.getItem(pos) as CollectVo
                curPos = pos
                when (view.id)
                {
                    R.id.iLLContent ->
                    { // 跳转详情
                        // "地点","线路","玩家产品"
                        val intent = Intent()
                        intent.putExtra("id", vo.pdtId)
                        if (position == 0)
                        {
                            intent.setClass(activity, ScenicActivity::class.java)
                        }
                        else if (position == 1)
                        {
                            intent.setClass(activity, RouteMainActivity::class.java)
                        }
                        else if (position == 2)
                        {
                            intent.setClass(activity, ProductDetailActivity::class.java)
                        }
                        start(intent)
                    }
                    R.id.iTvDelete  ->
                    { // 取消收藏
                        val body = BaseModel(true)
                        body.type = "NOCOL"
                        body.colId = vo.id
                        mPresenter.getData(HttpDto(Constant.COLLECT_ADD, body).setShowError(false))
                    }
                }
            }
        }
        adapter.emptyView = SampleHeader(activity, swipe_target.width, swipe_target.height)
        adapter.isUseEmpty(false)
        adapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN)
        swipe_target.addItemDecoration(DividerItemDecoration(DividerItemDecoration.VERTICAL_LIST,
                                                             R.color.transparent))
        swipe_target.layoutManager = LinearLayoutManager(activity)
        swipe_target.adapter = adapter
        // mRecyclerView.isNestedScrollingEnabled = false
        mSwipeToLoadLayout.setOnRefreshListener(this)
        mSwipeToLoadLayout.setOnLoadMoreListener(this)
        mSwipeToLoadLayout.isLoadMoreEnabled = true
        mSwipeToLoadLayout.isRefreshEnabled = true
    }

    override fun cancelSuccess()
    {
        adapter.remove(curPos)
    }

    // --------------- 数据获取 ---------------
    override fun onDestroy()
    {
        super.onDestroy()
        adapter.removeAllFooterView()
        sampleFooter = null
    }

    override fun getData(silence: Boolean)
    {
        isRefresh = false
        disLoading()
        val body = BaseModel(true)
        // "地点","线路","玩家产品"
        if (position == 0)
        {
            body.type = "PLACE"
        }
        else if (position == 1)
        {
            body.type = "ROUTE"
        }
        else if (position == 2)
        {
            body.type = "PRODUCT"
        }
        body.page = page.toString()
        mPresenter.getData(HttpDto(Constant.COLLECT_LIST, body).setType(HttpDto.GET).setTryAgain(
                true))
    }

    var sampleFooter: SampleFooter? = null
    override fun bindList(data: CollectListVo)
    {
        adapter.isUseEmpty(true)
        if (data.list != null && data.list.isNotEmpty())
        {
            if (page == 1)
            {
                adapter.replaceData(data.list)
            }
            else
            {
                adapter.addData(data.list)
            }
            if (data.pages == page)
            {
                sampleFooter
                        ?: let {
                            sampleFooter = SampleFooter(activity)
                        }
                adapter.setFooterView(sampleFooter)
                mSwipeToLoadLayout.isLoadMoreEnabled = false
            }
            page++
        }
        else
        {
            if (page == 1)
            {
                adapter.replaceData(arrayListOf())
                mSwipeToLoadLayout.isLoadMoreEnabled = false
            }
        }
    }

    private var page = 1
    override fun disLoading()
    {
        mSwipeToLoadLayout.isLoadingMore = false
        mSwipeToLoadLayout.isRefreshing = false
    }

    override fun onRefresh()
    {
        adapter.removeAllFooterView()
        page = 1
        mSwipeToLoadLayout.isLoadMoreEnabled = true
        mSwipeToLoadLayout.isRefreshEnabled = true
        getData(true)
    }

    override fun onLoadMore()
    {
        getData(true)
    }

    // --------------- EventBus ---------------
    private var isRefresh = false

    @Subscribe
    fun onEvent(event: String)
    {
        if (Constant.EVENT_REFRESH_ORDER == event)
        {
            isRefresh = true
        }
    }

}